package com.moss.cloud.base.biz.listener;

import com.moss.cloud.base.biz.event.ResourceChangeEvent;
import com.moss.cloud.common.core.constant.MsgTopic;
import com.moss.cloud.common.core.enums.MsgActionEnum;
import com.moss.cloud.common.core.enums.MsgTypeEnum;
import com.moss.cloud.common.core.model.dto.MessageDTO;
import com.moss.cloud.common.security.component.MessageEventSender;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 资源变更事件处理中心--此类消息必须是事务处理完成之后再调用
 *
 * @author 瑾年
 * @date 2023年11月21日
 */
@Slf4j
@Component
public class ResourceListener implements ApplicationListener<ResourceChangeEvent> {
    private final MessageEventSender messageEventSender;

    public ResourceListener(MessageEventSender messageEventSender) {
        this.messageEventSender = messageEventSender;
    }


    @Override
    public void onApplicationEvent(@NotNull ResourceChangeEvent event) {
        log.info("监听资源变更事件->变更类型:{}", event.getChangeType());
        messageEventSender.sendMessage(MsgTopic.BASE_RESOURCE_CHANGE, new MessageDTO(MsgTypeEnum.ONE.name(), MsgActionEnum.UPDATE.name(), event.getChangeType()));
    }
}
